﻿
using Tester.Models;
using Tester.Views;

namespace Tester.ViewModels;

internal class PersonalizeViewModels : IViewModels
{
    public List<NavItemModel> NavList { get; set; }
    public List<IWidget> Widgets { get; set; }

    public Views.CommonConfig CommonConfig { get; set; }

    public PersonalizeViewModels()
    {
        NavList = new();
        Widgets = new();
        CommonConfig = new();
        try
        {
            CommonConfig = Models.ReadAndWrite.Deserialize(Models.ReadAndWrite.FileRead());
        }
        catch (Exception ex)
        {
            DisplayAlert("提示", "未找到配置文件或配置文件不正确。\n将进行初始化。", "OK");
            InitConfig();
        }
        if (CommonConfig == null)
        {
            DisplayAlert("提示", "未找到配置文件或配置文件不正确。\n将进行初始化。", "OK");
            InitConfig();
        }
        InitializeWidget();
    }


    public void InitializeWidget()
    {
        Widgets.Add(new PickerWidget
        {
            Header = "更换主题",
            Description = "",
            Mode = 1,
            ItemCollection = CommonConfig.themes,
            PickerEventHandler = new((int i) =>
            {
            switch (i)
                {
                    case 0:
                        App.Current.UserAppTheme = AppTheme.Unspecified;
                        break;
                    case 1:
                        App.Current.UserAppTheme = AppTheme.Light;
                        break;
                    case 2:
                        App.Current.UserAppTheme = AppTheme.Dark;
                        break;
                    default:
                        App.Current.UserAppTheme = AppTheme.Unspecified;
                        break;
                }

            })
        });
        
    }

    private void InitConfig()
    {
        CommonConfig = new();
        CommonConfig.themes.Add("跟随系统");
        CommonConfig.themes.Add("浅色");
        CommonConfig.themes.Add("深色");
        Models.ReadAndWrite.Write(CommonConfig);
        try
        {
            CommonConfig = Models.ReadAndWrite.Deserialize(Models.ReadAndWrite.FileRead());
        }
        catch (Exception ex)
        {
            DisplayAlert("提示", "配置文件不正确。", "OK");
            throw new Exception();
        }
    }

    private void DisplayAlert(string title, string details, string accept)
    {
        Models.Message.Show("ShowDialog", title, details, accept);
    }

    private void PickerEventHandler1(object sender, EventArgs e)
    {
        var picker = (Picker)sender;
        int selectedIndex = picker.SelectedIndex;
        App.Current.UserAppTheme = AppTheme.Dark;

    }

    
}